home *** CD-ROM | disk | FTP | other *** search
/ Animation / Animation Vol.1 (Profi ROM)(1994).iso / pool / updates / symantec / rtlinc.exe / IOSTREAM.H < prev    next >
C/C++ Source or Header  |  1993-06-10  |  43KB  |  1,194 lines

  1. // IOStreams Package
  2. // Steve Teale April 1992
  3. // Copyright Symantec Corp 1990-1992. All Rights Reserved.
  4.  
  5. #ifndef __IOSTREAM_H
  6. #define __IOSTREAM_H
  7. #include <stddef.h>
  8. #include <2comp.h>
  9.  
  10. #define seek_dir relative_to
  11.  
  12. #ifndef EOF
  13. const int EOF = -1;
  14. #endif
  15.  
  16. const int _ios_default_decimal_precision = 6;
  17. const int _ios_n_extended_format_words = 10;
  18. // This is the number of extended format state words reserved for use
  19. // by derived classes and user inserters. This value should be reasonably
  20. // small, as it inflates the size of each instance of ios.
  21.  
  22. #pragma pack(__DEFALIGN)
  23. class streampos {
  24. friend class streamoff;
  25. public:
  26.     streampos(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  27.     operator long() const { return (ne == -1)? EOF: ne*es; }
  28. private:
  29.     long ne;
  30.     size_t es;
  31. };
  32.  
  33. class streamoff {
  34. public:
  35.     streamoff(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  36.     streamoff(streampos &a) : ne(a.ne), es(a.es) {}
  37.     size_t stepsize() const { return es; }
  38.     long steps() const { return ne == -1? 0: ne; }
  39.     streamoff& operator += (long o) { ne += o; return *this; }
  40.     streamoff& operator -= (long o) { ne -= o; return *this; }
  41.     operator long() const { return steps()*es; }
  42.  
  43. private:
  44.     long ne;
  45.     size_t es;
  46. };
  47.  
  48. class streambuf;
  49. class ostream;
  50. class istream;
  51.  
  52.  
  53. class ios {
  54.  
  55. // This is the base class for istream and ostream, and all of their
  56. // derivations.
  57.  
  58. friend ostream &endl(ostream &);
  59.  
  60. public:
  61.     enum io_state {
  62.         goodbit=0,
  63. // No errors - everything hunky dory!
  64.         eofbit=1,
  65. // Normally set when underflow failed because there was no more file.
  66.         failbit=2,
  67. // An error has ocurred, but it is probably recoverable, and the
  68. // stream is still in a useable state
  69.         badbit=4
  70. // A fatal error has ocurred
  71.     };
  72.  
  73. // This is called seek_dir in the AT & T version, which is misleading.
  74. // A define is included above for compatibility. These enumerators are
  75. // used to specify relative seeks in streams.
  76.     enum relative_to {
  77.         beg,
  78. // For seek operations relative to the beginning of the stream (file),
  79.  
  80.         cur,
  81. // relative to the current position in the stream (file),
  82.  
  83.         end
  84. // and relative to the end of the stream (file)
  85.     };
  86.  
  87. // The following enumeration applies to file related derivatives.
  88.     enum open_mode {
  89.         in=0x1,
  90. // Input allowed
  91.  
  92.         out=0x2,
  93. // Output allowed
  94.  
  95.         ate=0x4,
  96. // A seek to the end of the file to be performed during open.
  97.  
  98.         app=0x8,
  99. // All writes are to the end of the file - implies out
  100.  
  101.         trunc=0x10,
  102. // Existing contents of the file to be discarded. Implies out
  103. // unless ate or app specified as well.
  104.  
  105.         nocreate=0x20,
  106. // Open will fail if the file does not already exist
  107.  
  108.         noreplace=0x40,
  109. // Open will fail if the file does already exist
  110.  
  111.         translated = 0x80
  112. // CR/LF pairs to be translated to newline characters on input
  113. // and newline characters to be translated to CR/LF pairs on
  114. // output (the normal behaviour for DOS)
  115.     };
  116.  
  117. // The formatting state is a bit-mask used to control some of the
  118. // inserters and extractors. All of the bits of the format state
  119. // can be manipulated by flags(), setf(), and unsetf(). Some
  120. // specialized parts of the formatting state can be manipulated
  121. // by fill(), width(), and precision() . Here are the meanings
  122. // of the various bits:
  123.  
  124.     enum format_mode {
  125.         skipws = 0x1,
  126. // Skip past leading white space when extracting.
  127. // Since zero-width fields are considered an
  128. // error by the numeric extractors, attempting
  129. // to extract white-space into a number without
  130. // this bit set will set an error flag.
  131.  
  132.         left = 0x2,
  133. // Left-adjust values when inserting (fill on the right).
  134.  
  135.         right = 0x4,
  136. // Right-adjust values when inserting (fill on the left).
  137.  
  138.         internal = 0x8,
  139. // When inserting, fill _between_ the numeric sign or
  140. // base indicator and the value.
  141.  
  142.         dec = 0x10, oct = 0x20, hex = 0x40,
  143. // Default radix for integers. If neither dec, octal,
  144. // or hex is set, integer inserters use base 10, and
  145. // integer extractors interpret numbers according to the
  146. // C++ lexical convention: "0x" precedes a base-16 number,
  147. // and a number with a leading zero is base-8.
  148.  
  149.         showbase = 0x80,
  150. // If this is set, base-16 numbers will be inserted with a
  151. // leading "0x", and base-8 numbers will have a leading zero.
  152.  
  153.         showpoint = 0x100,
  154. // If this is set, the floating-point inserters will print a
  155. // decimal point and trailing zeroes, even when the trailing
  156. // places are not significant.
  157.  
  158.         uppercase = 0x200,
  159. // If this is set, "E" instead of "e" will be used to indicate
  160. // the exponent of a floating point number, and "A" through "F"
  161. // will be used to represent base-16 numerals instead of "a"
  162. // through "f".
  163. //
  164. // If uppercase and showbase are both set, the string "0X"
  165. // instead of "0x" will be used to indicate a base-16 number.
  166.  
  167.         showpos = 0x400,
  168. // If this is set, positive numbers will be inserted with a
  169. // leading "+".
  170.  
  171.         scientific = 0x800,
  172. // If this is set, the floating-point inserters will print a
  173. // number with one digit before the decimal point, and the
  174. // number of digits after the decimal point equal to the
  175. // value of precision(). The character "e" will introduce the
  176. // exponent.
  177.  
  178.         fixed = 0x1000,
  179. // If this is set, the floating-point inserters will use
  180. // precision() to determine the number of digits after the
  181. // decimal point.
  182. //
  183. // If neither scientific or fixed is set, numbers with
  184. // exponents smaller than -4 or greater than precision() will
  185. // be printed as if scientific were set. Other numbers will
  186. // be printed using zeroes to explicitly show the decimal place.
  187.  
  188.         unitbuf = 0x2000,
  189. // When this is set, a flush is performed after each insertion
  190. // (by ostream::osfx()). This is more efficient than using
  191. // unbuffered output, but provides most of the same advantages.
  192.  
  193.         stdio = 0x4000
  194. // When this is set, streams using stdiobufs will flush stdout and
  195. // stderr after each insertion.
  196.  
  197. // Note that it is not possible to use bit 0x8000 in this way,
  198. // as this evaluates to an enumerator with a negative value and
  199. // lots of bits set.
  200.     };   // end enum format_mode
  201.  
  202.     static const long stickywidth;
  203.     static const long spacing;
  204.  
  205.     enum format_mode_mask {
  206.         defaults = right|skipws,
  207.         basefield = dec|oct|hex,
  208.         adjustfield = left|right|internal,
  209.         floatfield = scientific|fixed
  210.     };
  211.  
  212. public: // just a reminder!
  213.  
  214.     ios(streambuf *buffer);
  215. // Construct an ios associated with the argument streambuf.
  216. // "buffer" should not be null.
  217.  
  218.     virtual ~ios();
  219.  
  220. /////////////////////////////////////////////////////////////
  221. //
  222. // Functions to interrogate/set the error state
  223.  
  224.     int good() const { return error_state == 0; };
  225. // If there are no error bits set, this returns non-zero, otherwise
  226. // it returns zero.
  227.  
  228.     int eof() const { return error_state & eofbit; };
  229. // If eofbit is set in the error state, this return non-zero, otherwise
  230. // it returns zero. This indicates that end-of-file has been reached
  231. // while reading the character stream.
  232.  
  233.     int fail() { return error_state & (badbit | failbit); }
  234. // If badbit or failbit is set in the error state, return non-zero.
  235. // Otherwise, return zero. Failbit generally indicates that some
  236. // extraction has failed, but the stream may still be used once
  237. // failbit has been cleared.
  238.  
  239.     int bad() const { return error_state & badbit; }
  240. // Returns non-zero if badbit is set in the error state. This
  241. // indicates some unrecoverable error, generally an I/O error.
  242.  
  243.     int operator!() const
  244.     {
  245.         return error_state & (badbit|failbit);
  246.     }
  247. // Return non-zero if badbit or failbit is set in the error state,
  248. // which allows expressions of the form:
  249. //  if ( !cout )
  250.  
  251.     operator void*()
  252.     {
  253.         return (error_state & (badbit|failbit))?
  254.             0: this;
  255.     }
  256. // Convert an ios to a value th